package forum.business.appservices;

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;

import forum.business.dao.UserDAO;
import forum.business.entities.UserBO;
import forum.util.exceptions.AdException;

public class UserAppService {

	public UserAppService() {
	}

	public void createUser(UserBO userBO) throws AdException {
		UserDAO userDAO = new UserDAO();
		try {
			userDAO.begin();
			userDAO.createUser(userBO);
			userDAO.commit();
		} catch (HibernateException e) {
			userDAO.rollback();
			throw new AdException("Problem z utworzeniem uzytkownika ", e);

		}
	}

	public UserBO getUser(String login) throws AdException {
		UserDAO userDAO = new UserDAO();
		try {
			userDAO.begin();
			UserBO userBO = userDAO.getUser(login);
			userDAO.commit();
			if (userBO == null)
				throw new AdException("Nie ma uzytkownika o podanym loginie ");
			return userBO;
		} catch (HibernateException e) {
			userDAO.rollback();
			throw new AdException("Problem z pobraniem uzytkownika ", e);
		}

	}

	public List<UserBO> listUsers() throws AdException {
		UserDAO userDAO = new UserDAO();
		try {
			userDAO.begin();
			List<UserBO> users = userDAO.listUsers();
			userDAO.commit();
			return users;
		} catch (HibernateException e) {
			userDAO.rollback();
			throw new AdException("Problem z wy�wietleniem u�ytkownik�w ", e
					.getCause());
		}
	}

	public void removeUser(String login) throws AdException {
		UserDAO userDAO = new UserDAO();
		try {
			userDAO.begin();
			userDAO.removeUser(login);
			userDAO.commit();
		} catch (HibernateException e) {
			userDAO.rollback();
			throw new AdException("Problem z usuni�ciem u�ytkownika ", e
					.getCause());
		}
	}

	public void updateUser(UserBO userBO) throws AdException {
		UserDAO userDAO = new UserDAO();
		try {
			userDAO.begin();
			userDAO.updateUser(userBO);
			userDAO.commit();
		} catch (HibernateException e) {
			userDAO.rollback();
			throw new AdException("Problem z uaktualnieniem u�ytkownika ", e
					.getCause());
		}
	}

}
